package bzimerman.media.myflix;

import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;


import android.content.Context;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;



public class movieDatabase extends SQLiteOpenHelper{

	private static final String DATABASE_NAME = "myFlixDatabase";
	
	private static final int DATABASE_VERSION = 36;
    private static final String MOVIE_TABLE_NAME = "MovieFiles";
    private static final String MOVIE_TABLE_CREATE =
                "CREATE TABLE " + MOVIE_TABLE_NAME + " ( _id TEXT PRIMARY KEY, Description TEXT, " +
                		"Rating Integer, Watched Integer, Picture TEXT);";
    
    private static final String GENRE_TABLE_NAME = "Genres";
    private static final String GENRE_TABLE_CREATE =
                "CREATE TABLE " + GENRE_TABLE_NAME + " ( _id TEXT PRIMARY KEY, Action Boolean, " +
                		"Adventure Boolean, Animation Boolean, Anime Boolean, Biography Boolean, Comedy Boolean, Crime Boolean," +
                		"Documentary Boolean, Drama Boolean, Family Boolean, Fantasy Boolean, Film-Noir Boolean," +
                		"Game-Show Boolean, History Boolean, Horror Boolean, Music Boolean, Musical Boolean," +
                		"Mystery Boolean, News Boolean, Reality-TV Boolean, Romance Boolean, Sci-Fi Boolean," +
                		"Sport Boolean, Talk-Show Boolean, Thriller Boolean, War Boolean, Western Boolean);";
    
movieDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    System.out.println(context.toString());
}

@Override
public void onCreate(SQLiteDatabase db) {
	db.execSQL(MOVIE_TABLE_CREATE);
    db.execSQL(GENRE_TABLE_CREATE);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	  // TODO Auto-generated method stub
	  
	  db.execSQL("DROP TABLE IF EXISTS "+MOVIE_TABLE_NAME);
	  db.execSQL("DROP TABLE IF EXISTS "+GENRE_TABLE_NAME);
	  onCreate(db);
}

public String convertToHash(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException{
	String key = messageHash.SHA1(text);
	return key;
}

public void InsertMovie(String File,
		String Path, String Description, int Rating, 
		int Watched, String Picture)throws NoSuchAlgorithmException, UnsupportedEncodingException{
	//The movies need to be hashed to ensure that the same movie isn't inserted twice 
	String key = convertToHash(Path + File);
	System.out.println(key);
	SQLiteDatabase db;
	 if (!returnUsed(key, MOVIE_TABLE_NAME)){
			db=this.getWritableDatabase();
		 	db.execSQL("INSERT INTO "
	         + MOVIE_TABLE_NAME
	         + " ( _id , Description , Rating , Watched , Picture )"
	         + " VALUES ("+key+","+Description+","+ Rating +","+Watched+","+Picture+
             		");");
		 	db.close();
	 }
	 else{
		 db=this.getWritableDatabase();
		 db.execSQL("UPDATE "
		         + MOVIE_TABLE_NAME
		         + " SET Description = " + Description + ", " +
		         		"Rating = " + Rating + ", " +
		         		"Watched = " + Watched + ", " +
		         		"Picture = " + Picture +
		         		"WHERE _id = "+ key +";");
	 	db.close();
	 }
}

public boolean returnUsed(String position, String Dictionary){
	System.out.println("Got there");
	int count;
	try{
		SQLiteDatabase db=this.getReadableDatabase();
	}catch(Exception e){
		Log.v("Excepqqqqqqqqqqqqqqqqqqqq", ""+e);
	}	
	SQLiteDatabase db=this.getReadableDatabase();
	System.out.println("Got this");
	SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT * from "+ Dictionary +" WHERE _id = " + position + ";",new String [] {});
	System.out.println("Got that");
	count = c.getCount();
	db.close();
	
	System.out.println("Got here");
	
	if (count> 0){
		return true;
	}
	else{
		return false;
	}
}

public int returnRating(String File,String Path) throws NoSuchAlgorithmException, UnsupportedEncodingException{
	int count;
	String key = convertToHash(Path + File);
	SQLiteDatabase db=this.getReadableDatabase();
	SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT * from "+ MOVIE_TABLE_NAME +" WHERE _id = " + key + ";",new String [] {});
	count = c.getCount();
	db.close();
	
	if (count> 0){
		c.moveToFirst();
		int[] values = {c.getInt(0),c.getInt(1),c.getInt(2),c.getInt(3),c.getInt(4)};
		return values[2];
	}
	else{
		return 0;
	}
}

}